package org.openzip.openzip.gui.util;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.filechooser.FileSystemView;

import org.openzip.openzip.gui.util.layout.FileIconFactory;

/**
 * A kind of hack is used to create and paint the checkbox. In order to display
 * both the file icon and the file name, the checkbox is created without the
 * text label. A new label (instance of <code>JLabel</code>) is then created
 * with the label and the file name. Because the <code>FileCheckBox</code> is
 * a child of <code>JCheckBox</code>, which is a child of
 * <code>JComponent</code> and it is a child of <code>Container</code>, the
 * label is then added to the checkbox. The label has to be moved right by
 * rougly 15 pixels, or it gets painted over the checkbox.
 */
public class FileCheckBox extends ObserverThreestateCheckBox {

	private File file;

	private static Map<String, Icon> iconCache = new HashMap<String, Icon>();

	public FileCheckBox(File file) {
		super();
		this.file = file;

		String text = file.getName();
		if (text == null || text.equals("")) {
			text = file.getAbsolutePath();
		}
		JLabel label = new JLabel(text);

		Icon icon = this.iconCache.get(FileIconFactory.getFileExtension(file));
		if (icon == null) {
			// System.out.println("Getting icon of " + file.getName());

			String ext = FileIconFactory.getFileExtension(file);
			// We want to exclude directories from cache as they might have
			// different icons
			if (ext != "") {
				icon = FileIconFactory.getIconByExtension(ext);
				this.iconCache.put(ext, icon);
			}

		}

		// Icon fileIcon =
		// FileSystemView.getFileSystemView().getSystemIcon(file);
		label.setIcon(icon);
		label.setBorder(BorderFactory.createEmptyBorder(0, 16, 0, 0));

		add(label);
	}

	

	public File getFile() {
		return this.file;
	}

}
